<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <!-- <el-form-item label="通知时间" prop="noticeTime">
        <el-input v-model="dataForm.noticeTime" placeholder="通知时间"></el-input>
      </el-form-item> -->
          <el-form-item label="通知类型" prop="type">
            <el-radio-group v-model="dataForm.type">
                <el-radio :label="1">公告</el-radio>
                <el-radio :label="2">通知</el-radio>
                <el-radio :label="3">工作消息</el-radio>
                <el-radio :label="4">系统消息</el-radio>
               
              </el-radio-group>
      </el-form-item>
          <el-form-item label="通知消息" prop="msg">
        <el-input v-model="dataForm.msg" placeholder="通知消息"></el-input>
      </el-form-item>
      <el-form-item label="用户" >
            <el-select class="w100" @change="jxcrtxr" v-model="dataForm.userId" placeholder="请选择用户" clearable
              filterable>
              <el-option v-for="item in xczxtrt" 
              :key="item.id" :label="item.realName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="是否阅读" prop="isRead">
        <el-input v-model="dataForm.isRead" placeholder="是否阅读"></el-input>
      </el-form-item> -->
          <!-- <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item> -->
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);
import { dxget } from "@/utils/utils";
const xczxtrt = ref()
const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '',  noticeTime: '',  type: '',  msg: '',  userId: '',  isRead: '',  createTime: ''});

const rules = ref({
          noticeTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          type: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          msg: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          userId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          isRead: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          createTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/notice/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/api/notice", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
const jmkxcf = async () => {

const { list } = await dxget('/api/user/page', {
  limit: 999,
  isAuthentication:true
})
xczxtrt.value = list
}
jmkxcf()
</script>
